/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.doc.sender.ui;

import com.database.entity.DocumentSources;
import database.senderUtil;
import java.awt.Point;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author parent
 */
public class DocSourceFrame extends javax.swing.JFrame {
    private int Tag;
    private int selectedSourceID;
    int[] RecordId=new int[200];
    int RecPos;
    int MaxRecord;
    boolean inserted;
    public int getTag() {
        return Tag;
    }

    public void setTag(int Tag) {
        this.Tag = Tag;
    }
    private boolean edited;
    /**
     * Creates new form sourceFrame
     */
//    CRUDPanel crudPanel;
    public DocSourceFrame() {
//        crudPanel=new CRUDPanel();
//        crudPanel.setVisible(true);
//        getContentPane().add(crudPanel,java.awt.BorderLayout.NORTH);
        initComponents();
        edited=false;
        setButtonEnabled(edited);
       
        RecPos=0;
        MaxRecord=0;
    }
    private void setButtonEnabled(boolean edited){
        btnAdd.setEnabled(!edited);
        btnEdit.setEnabled(!edited);
        btnSave.setEnabled(edited);
        btnCancel.setEnabled(edited);
        btnRemove.setEnabled(!edited);
        panelDetil.setEnabled(edited);
        if (edited)jTextField1.requestFocus();
    }
    public void LoadTable(String keyword){
        
        Session session = senderUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            if (keyword.isEmpty()){
                String queryString= "from DocumentSources order by source_name asc";
                Query query = session.createQuery(queryString);
                List listRecord=query.list();
                if (listRecord!=null){
                MaxRecord=listRecord.size();
                displayResult(listRecord);
                }
            } else {
                String queryString= "from DocumentSources where source_name like :kw "
                        + "order by source_name asc";
                Query query = session.createQuery(queryString);
                query.setString("kw", "%"+keyword+"%");
                List listRecord=query.list();
                if (listRecord!=null){
                MaxRecord=listRecord.size();
                displayResult(listRecord);
                }
            }
            RecPos=0;
            transaction.commit();
            session.close();
            
        } catch (Exception e){
            //return false;
            e.printStackTrace();
        }  
    }    
    
    public DocumentSources getDocumentSourceRecords(int id){
        Session session = senderUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            String queryString = "from DocumentSources where id=:id";
            Query query = session.createQuery(queryString);
            query.setInteger("id", id);
            DocumentSources sources=(DocumentSources)query.uniqueResult();
            displayRecord(sources);
            transaction.commit();
            session.close();
            if(sources!=null){
                // System.out.println("File sudah ada");
                return sources;
               
            } else return null;
            
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    public void DeleteRecord(int id){
        Session session = senderUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            String queryString = "from DocumentSources where id=:id";
            Query query = session.createQuery(queryString);
            query.setInteger("id", id);
            DocumentSources sources=(DocumentSources)query.uniqueResult();
            session.delete(sources);
            transaction.commit();
            session.close();
             
            
        } catch (Exception e){
            e.printStackTrace();
             
        }
    }
    private void displayRecord(DocumentSources sources){
        jTextField1.setText(sources.getSourceName());
        jTextField2.setText(sources.getDescription());
        jTextField3.setText(sources.getCity());
        jTextField4.setText(sources.getPostalCode());
        jTextField5.setText(sources.getTelp());
        jTextField6.setText(sources.getFax());
        jTextArea1.setText(sources.getAddress());
    }
    private void setBlankRecord(){
        jTextField1.requestFocus();
        jTextField1.setText("");
        jTextField2.setText("");
        jTextField3.setText("");
        jTextField4.setText("");
        jTextField5.setText("");
        jTextField6.setText("");
        jTextArea1.setText("");
    }
    private void displayResult(List resultList) {
    Vector<String> tableHeaders = new Vector<String>();
    Vector tableData = new Vector();
    tableHeaders.add("Id"); 
    tableHeaders.add("Nama Asal");
    tableHeaders.add("Keterangan");
    tableHeaders.add("Alamat");
    tableHeaders.add("Kota");
    tableHeaders.add("Kode Pos");
    tableHeaders.add("Telepon");
    tableHeaders.add("Faximile");
    
//    DefaultTableModel model= (DefaultTableModel)resultTable.getModel();
    int i=0;
    
    for(Object o : resultList) {
        DocumentSources sources = (DocumentSources)o;
        Vector<Object> oneRow = new Vector<Object>();
        oneRow.add(sources.getId());
        RecordId[i]=sources.getId();
        oneRow.add(sources.getSourceName());
        //oneRow.add("[BLOB "+Integer.toString(PdfFiles.getId())+"]");
        oneRow.add(sources.getDescription());
        oneRow.add(sources.getAddress());
        oneRow.add(sources.getCity());
        oneRow.add(sources.getPostalCode());
        oneRow.add(sources.getTelp());
        oneRow.add(sources.getFax());
        tableData.add(oneRow);
        i++;
    }
    resultTable.setModel(new DefaultTableModel(tableData, tableHeaders));
  
}
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        editorMenu = new javax.swing.JPopupMenu();
        mnuEdit = new javax.swing.JMenuItem();
        mnuDelete = new javax.swing.JMenuItem();
        mnuShow = new javax.swing.JMenuItem();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        btnRemove = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        btnFirst = new javax.swing.JButton();
        btnPrev = new javax.swing.JButton();
        btnNext = new javax.swing.JButton();
        btnLast = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        Keyword = new javax.swing.JTextField();
        btnSearch = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        tabRecord = new javax.swing.JTabbedPane();
        panelDetil = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jTextField2 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        panelList = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        resultTable = new javax.swing.JTable();

        mnuEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/doc/sender/ui/gfx/edit.gif"))); // NOI18N
        mnuEdit.setText("Edit Record");
        mnuEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuEditActionPerformed(evt);
            }
        });
        editorMenu.add(mnuEdit);

        mnuDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/doc/sender/ui/gfx/delete.gif"))); // NOI18N
        mnuDelete.setText("Hapus");
        mnuDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuDeleteActionPerformed(evt);
            }
        });
        editorMenu.add(mnuDelete);

        mnuShow.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/doc/sender/ui/gfx/zoom.png"))); // NOI18N
        mnuShow.setText("Lihat Detail");
        mnuShow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuShowActionPerformed(evt);
            }
        });
        editorMenu.add(mnuShow);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Data Asal Dokumen");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jToolBar1.setRollover(true);

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/doc/sender/ui/gfx/add.gif"))); // NOI18N
        btnAdd.setText("New");
        btnAdd.setFocusable(false);
        btnAdd.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnAdd.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        jToolBar1.add(btnAdd);

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/doc/sender/ui/gfx/edit.gif"))); // NOI18N
        btnEdit.setText("Edit");
        btnEdit.setFocusable(false);
        btnEdit.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEdit.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        jToolBar1.add(btnEdit);

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/doc/sender/ui/gfx/save.gif"))); // NOI18N
        btnSave.setText("Save");
        btnSave.setFocusable(false);
        btnSave.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSave.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        jToolBar1.add(btnSave);

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/doc/sender/ui/gfx/close.gif"))); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.setFocusable(false);
        btnCancel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCancel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        jToolBar1.add(btnCancel);

        btnRemove.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/doc/sender/ui/gfx/delete.gif"))); // NOI18N
        btnRemove.setText("Remove");
        btnRemove.setFocusable(false);
        btnRemove.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnRemove.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveActionPerformed(evt);
            }
        });
        jToolBar1.add(btnRemove);
        jToolBar1.add(jSeparator1);

        btnFirst.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/doc/sender/ui/gfx/first.gif"))); // NOI18N
        btnFirst.setText("First");
        btnFirst.setFocusable(false);
        btnFirst.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnFirst.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnFirst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFirstActionPerformed(evt);
            }
        });
        jToolBar1.add(btnFirst);

        btnPrev.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/doc/sender/ui/gfx/prev.gif"))); // NOI18N
        btnPrev.setText("Prev");
        btnPrev.setFocusable(false);
        btnPrev.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnPrev.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnPrev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrevActionPerformed(evt);
            }
        });
        jToolBar1.add(btnPrev);

        btnNext.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/doc/sender/ui/gfx/next.gif"))); // NOI18N
        btnNext.setText("Next");
        btnNext.setFocusable(false);
        btnNext.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnNext.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });
        jToolBar1.add(btnNext);

        btnLast.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/doc/sender/ui/gfx/last.gif"))); // NOI18N
        btnLast.setText("Last");
        btnLast.setFocusable(false);
        btnLast.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnLast.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnLast.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLastActionPerformed(evt);
            }
        });
        jToolBar1.add(btnLast);
        jToolBar1.add(jSeparator2);
        jToolBar1.add(jSeparator3);

        Keyword.setAutoscrolls(false);
        Keyword.setPreferredSize(new java.awt.Dimension(150, 40));
        jToolBar1.add(Keyword);

        btnSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/doc/sender/ui/gfx/zoom.png"))); // NOI18N
        btnSearch.setText("Search");
        btnSearch.setFocusable(false);
        btnSearch.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSearch.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });
        jToolBar1.add(btnSearch);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 567, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jSplitPane1.setLeftComponent(jPanel1);

        panelDetil.setEnabled(false);

        jLabel2.setText("Nama Asal Dokumen :");

        jLabel3.setText("Alamat :");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        jLabel4.setText("Deskripsi  :");

        jLabel5.setText("Kota :");

        jLabel6.setText("Kode Pos :");

        jLabel7.setText("Telepon :");

        jLabel8.setText("Faximile :");

        javax.swing.GroupLayout panelDetilLayout = new javax.swing.GroupLayout(panelDetil);
        panelDetil.setLayout(panelDetilLayout);
        panelDetilLayout.setHorizontalGroup(
            panelDetilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelDetilLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelDetilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelDetilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelDetilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelDetilLayout.createSequentialGroup()
                        .addComponent(jTextField3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jTextField6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
                    .addComponent(jTextField5, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap(151, Short.MAX_VALUE))
        );
        panelDetilLayout.setVerticalGroup(
            panelDetilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelDetilLayout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(panelDetilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelDetilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelDetilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelDetilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelDetilLayout.createSequentialGroup()
                        .addGroup(panelDetilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panelDetilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panelDetilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8)))
                    .addGroup(panelDetilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6)))
                .addContainerGap(30, Short.MAX_VALUE))
        );

        tabRecord.addTab("Detil", panelDetil);

        resultTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        resultTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                resultTableMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(resultTable);

        javax.swing.GroupLayout panelListLayout = new javax.swing.GroupLayout(panelList);
        panelList.setLayout(panelListLayout);
        panelListLayout.setHorizontalGroup(
            panelListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 572, Short.MAX_VALUE)
        );
        panelListLayout.setVerticalGroup(
            panelListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelListLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 25, Short.MAX_VALUE))
        );

        tabRecord.addTab("List", panelList);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabRecord, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabRecord)
        );

        jSplitPane1.setRightComponent(jPanel3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1)
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        setTag(1);
        edited = true;
        setButtonEnabled(edited);
        setBlankRecord();
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        setTag(2);
        edited = true;
        setButtonEnabled(edited);
    }//GEN-LAST:event_btnEditActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        setTag(3);
        edited = false;
        setButtonEnabled(edited);
        try {
            Session session = senderUtil.getSessionFactory().openSession();
            Transaction transaction = session.getTransaction();
            session.beginTransaction();
            String queryString = "from DocumentSources where source_name=:s";
            Query query = session.createQuery(queryString);
            query.setString("s", jTextField1.getText());
            DocumentSources sources=(DocumentSources)query.uniqueResult();
            if (sources==null) {
            sources=new DocumentSources();    
            inserted=true;
            } else inserted=false;
            sources.setSourceName(jTextField1.getText());
            sources.setDescription(jTextField2.getText());
            sources.setAddress(jTextArea1.getText());
            sources.setCity(jTextField3.getText());
            sources.setPostalCode(jTextField4.getText());
            sources.setTelp(jTextField5.getText());
            sources.setFax(jTextField6.getText());
            if (inserted){
                session.save(sources);
            } else {
                session.update(sources);
            }
            
            transaction.commit();
            session.close();
            LoadTable("");  
        } catch (Exception e){
            
        }
        
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        edited = false;
        setButtonEnabled(edited);
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveActionPerformed
        // TODO add your handling code here:
        setTag(4);
        JOptionPane dialog=new JOptionPane();
        int retval=dialog.showConfirmDialog(this, 
                "Yakin ingin menghapus Record "+Integer.toString(selectedSourceID)
                +" ini","Konfirmasi", JOptionPane.YES_NO_OPTION);
        if (retval==JOptionPane.YES_OPTION) {
            
        }
    }//GEN-LAST:event_btnRemoveActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
         LoadTable("");
    }//GEN-LAST:event_formWindowOpened

    private void resultTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_resultTableMouseReleased
        // TODO add your handling code here:
        DefaultTableModel model=(DefaultTableModel)resultTable.getModel();
        int row=resultTable.rowAtPoint(new Point(evt.getX(),evt.getY()));
        selectedSourceID=(int)model.getValueAt(row, 0);
        if (evt.isPopupTrigger()){
            editorMenu.show(evt.getComponent(),evt.getX(),evt.getY());
            
        }
    }//GEN-LAST:event_resultTableMouseReleased

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        // TODO add your handling code here:
        LoadTable(Keyword.getText());
    }//GEN-LAST:event_btnSearchActionPerformed

    private void btnFirstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFirstActionPerformed
        // TODO add your handling code here:
        RecPos=0;
        selectedSourceID=RecordId[RecPos];
        getDocumentSourceRecords(selectedSourceID);
        
    }//GEN-LAST:event_btnFirstActionPerformed

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        // TODO add your handling code here:
         
        if (RecPos<MaxRecord){
           RecPos++;
        }
        selectedSourceID=RecordId[RecPos];
        getDocumentSourceRecords(selectedSourceID);
    }//GEN-LAST:event_btnNextActionPerformed

    private void btnLastActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLastActionPerformed
        // TODO add your handling code here:
        RecPos=MaxRecord-1;
        
        selectedSourceID=RecordId[RecPos];
        getDocumentSourceRecords(selectedSourceID);
        
    }//GEN-LAST:event_btnLastActionPerformed

    private void btnPrevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrevActionPerformed
        // TODO add your handling code here:
        
        if (RecPos>0){
            RecPos--;
        }
        selectedSourceID=RecordId[RecPos];
        getDocumentSourceRecords(selectedSourceID);
    }//GEN-LAST:event_btnPrevActionPerformed

    private void mnuShowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuShowActionPerformed
        // TODO add your handling code here:
        getDocumentSourceRecords(selectedSourceID);
    }//GEN-LAST:event_mnuShowActionPerformed

    private void mnuDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuDeleteActionPerformed
        // TODO add your handling code here:
        btnRemoveActionPerformed(evt);
    }//GEN-LAST:event_mnuDeleteActionPerformed

    private void mnuEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuEditActionPerformed
        // TODO add your handling code here:
        getDocumentSourceRecords(selectedSourceID);
        btnEditActionPerformed(evt);
    }//GEN-LAST:event_mnuEditActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DocSourceFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DocSourceFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DocSourceFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DocSourceFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new DocSourceFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField Keyword;
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnFirst;
    private javax.swing.JButton btnLast;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnPrev;
    private javax.swing.JButton btnRemove;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnSearch;
    private javax.swing.JPopupMenu editorMenu;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JMenuItem mnuDelete;
    private javax.swing.JMenuItem mnuEdit;
    private javax.swing.JMenuItem mnuShow;
    private javax.swing.JPanel panelDetil;
    private javax.swing.JPanel panelList;
    private javax.swing.JTable resultTable;
    private javax.swing.JTabbedPane tabRecord;
    // End of variables declaration//GEN-END:variables
}
